﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Jungle.Code.DAL;

namespace Jungle.Prodamo.PriceManager
{
    public class Model
    {
        private int modelId;
        private int producerId;
        private string title;
        private string imageUrl;

        public List<Model> ModelList;

        public int ModelId { get { return modelId; } set { modelId = value; } }
        public int ProducerId { get { return producerId; } set { producerId = value; } }
        public string Title { get { return title; } set { title = value; } }
        public string ImageUrl { get { return imageUrl; } set { imageUrl = value; } }

        public void Add()
        { 
            using(SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Model_Add", connect);
                SqlDataReader reader = null;
                command.Parameters.Add("@ProducerId", SqlDbType.Int).Value = producerId;
                command.Parameters.Add("@Title", SqlDbType.NChar, 64).Value = title;
                command.Parameters.Add("@ImageUrl", SqlDbType.Text).Value = imageUrl;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    modelId = Convert.ToInt32(reader.GetValue(0));
                }
                else modelId = -1;
                command.Connection.Close();
            }
        }
        public void Delete()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Model_Delete", connect);
                command.Parameters.Add("@ModelId", SqlDbType.Int).Value = modelId;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        public void GetById()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Model_GetById", connect);
                SqlDataReader reader = null;

                command.Parameters.Add("@ModelId", SqlDbType.Int).Value = modelId;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while(reader.Read())
                {
                    modelId = (int)reader["ModelId"];
                    producerId = (int)reader["ProducerId"];
                    title = reader["Title"].ToString().Trim();
                    imageUrl = reader["ImageUrl"].ToString().Trim();
                }
                reader.Close();
                command.Connection.Close();
            }
        }
        public void GetByProducer()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                ModelList = new List<Model>();
                SqlCommand command = new SqlCommand("Model_GetByProducer", connect);
                SqlDataReader reader = null;
                command.Parameters.Add("@ProducerId", SqlDbType.Int).Value = producerId;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Model md = new Model();
                    md.modelId = (int)reader["ModelId"];
                    md.GetById();

                    ModelList.Add(md);
                }
                command.Connection.Close();
            }
        }
        public void GetByTitle()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                ModelList = new List<Model>();
                SqlCommand command = new SqlCommand("Model_GetByTitle", connect);
                SqlDataReader reader = null;
                command.Parameters.Add("@Title", SqlDbType.NChar, 64).Value = title;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                modelId = -1;
                while (reader.Read())
                {
                    modelId = 0;
                    Model md = new Model();
                    md.modelId = (int)reader["ModelId"];
                    md.producerId = (int)reader["ProducerId"];
                    md.title = reader["Title"].ToString().Trim();
                    md.imageUrl = reader["ImageUrl"].ToString().Trim();

                    ModelList.Add(md);
                }
                command.Connection.Close();
            }
        }

    }
}